/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package clases;

import java.awt.*;
import java.io.*;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.print.Doc;
import javax.print.DocFlavor;
import javax.print.DocPrintJob;
import javax.print.PrintException;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.SimpleDoc;
import javax.swing.ImageIcon;

/**
 *
 * @author USUARIO
 */
public class imprimir {
    public static String[] leerFichero(String n){
        String f[] = new String[100];
        String desc = "c:/is2/desc/" + n + ".txt";
        FileInputStream arc;
        int v;
        int index = 1;
        try {
            try {
                arc = new FileInputStream(desc);
                f[1] = "";
                while ((v = arc.read()) != -1){
                    if((char)v != '\n') f[index] += (char) v;
                    else{
                        index++;
                        f[index] = "";
                    }
                }
                arc.close();
            } catch (FileNotFoundException ex) {
                ex.printStackTrace();
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        System.out.println(index);
        f[0] = Integer.toString(index);
        return f;
    }
    
    public static void impresionReceta(Receta r){
        Frame f = new Frame ("Imprimir Receta");
        f.pack();
        PrintJob pjob = f.getToolkit().getPrintJob(f,"Impresion de la receta",null);
        Graphics pg = pjob.getGraphics();
        pg.setFont(new Font("SansSerif",Font.PLAIN,12));
        
        String dir = "c:/is2/fotos/"+Integer.toString(r.getId())+"ico.png";
        ImageIcon icon = new ImageIcon(dir,"Foto de la Receta");
        Image img = Toolkit.getDefaultToolkit().getImage(dir);
        pg.drawImage(img, 300, 40, null);
        
        String s1 = "Nombre: "+r.getNombre();
        pg.drawString(s1,50,50);
        String s2 = "Dificultad: "+r.getDificultad();
        pg.drawString(s2,50,65);
        String s3 = "Duración: "+r.getDuracion()+"mins.";
        pg.drawString(s3,50,80);
        String s4 = "Aporte energético: "+r.getKcal()+"KCals.";
        pg.drawString(s4,50,95);
        String s5 = "Nacionalidad: "+r.getNacionalidad().getNombre();
        pg.drawString(s5,50,110);
        String s6 = "Descricion: ";
        String s8 = new mostrarReceta().leerFichero(""+r.getId());/**/
        pg.drawString(s8,60,125);
        pg.drawString(s6,50,250);
        String s7[] = leerFichero(Integer.toString(r.getId()));
        
        int i = 1;
        while(s7[i] != null){
            pg.drawString(s7[i],60,250+(i*15));
            i++;
        }
        
        pg.dispose();
        pjob.end();
    }
    public void impresionIngs(Receta r){
        Frame f = new Frame ("Imprimir ingredientes");
        f.pack();
        PrintJob pjob = f.getToolkit().getPrintJob(f,"Impresion de los ingredientes",null);
        Graphics pg = pjob.getGraphics();
        pg.setFont(new Font("SansSerif",Font.PLAIN,12));
        
        int i = 0;
        Vector<Ingrediente> ing = (Vector)r.getIngredientes();
        
        do{
            Ingrediente uno = ing.elementAt(i);
            String s = uno.getProducto().getNombre()+": "+uno.getCantidad()+uno.getUnidad();
            pg.drawString(s,60,20+(i*15));
            i++;
        }while(i < ing.size());
        
        pg.dispose();
        pjob.end();
    }
}
